package com.glory.dmegcpda.view

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.dmegcpda.R
import com.glory.dmegcpda.databinding.ActivityMaterialReceiveBinding
import com.glory.dmegcpda.utils.PopWindowsUtils
import com.glory.dmegcpda.viewmodel.MaterialReceiveViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils
import kotlinx.android.synthetic.main.activity_bind_battery.*
import kotlinx.android.synthetic.main.activity_material_receive.*
import kotlinx.android.synthetic.main.activity_material_receive.btnConfirm
import kotlinx.android.synthetic.main.activity_material_receive.btnSubmit
import kotlinx.android.synthetic.main.activity_material_receive.etLot
import kotlinx.android.synthetic.main.activity_material_receive.etNum
import kotlinx.android.synthetic.main.activity_material_receive.ivBack
import kotlinx.android.synthetic.main.activity_material_receive.tvLotID
import kotlinx.android.synthetic.main.activity_material_receive.tvMaterialID

/**
 * FileName: MaterialReceiveActivity
 * Author: XJH
 * Date: 2021/9/27 11:01
 * Description:
 * History:
 */
class MaterialReceiveActivity : BaseActivity<MaterialReceiveViewModel>(), View.OnClickListener {
    var mBinding : ActivityMaterialReceiveBinding? = null
    private var getMaterialPop : PopWindowsUtils.GeneralPop? = null
    private var putMaterialPop : PopWindowsUtils.GeneralPop? = null
    override fun initData() {
        viewModel.getPopInfo()
        viewModel.defUI.callObserve.observe(this){
            tvGetMaterial.text = ""
            tvPutMaterial.text = ""
            tvMaterialID.text = ""
            tvLotID.text = ""
            tvMaterial.text = ""
            etLot.setText("")
            etNum.setText("")
            etPartCode.setText("")
            etLot.requestFocus()
        }

        viewModel.defUI.startActivity.observe(this){
            finish()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_material_receive)
        mBinding!!.materialReceiveViewmodel = viewModel
        etLot.requestFocus()
        ivBack.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
        btnConfirm.setOnClickListener(this)
        flGetMaterial.setOnClickListener(this)
        flPutMaterial.setOnClickListener(this)
//        FreeApi_EditTextUtils.setEditTextListener(etLot, this) {
//            val split = it.split("|")
//            if (split.isEmpty() || split.size < 3){
//                viewModel.defUI.toastEvent.value = ToastMessageBean("扫描编码格式有误，请重新扫描",false)
//                etLot.setText("")
//                etLot.requestFocus()
//                return@setEditTextListener
//            }
//
//            tvMaterialID.text = split[0]
//            tvLotID.text = split[1]
//            tvMaterial.text = split[2]
//        }

        etLot.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable) {
                val toString = text.toString()
                FreeApi_LogUtils.saveErrorMessages(toString, "绿码")
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etLot.setText(toString.replace("\r", "").replace("\n", ""));
                    val split = text.split("|")
                    if (split.isEmpty() || split.size < 3){
                        viewModel.defUI.toastEvent.value = ToastMessageBean("扫描编码格式有误，请重新扫描",false)
                        etLot.setText("")
                        etLot.requestFocus()
                        return
                    }

                    tvMaterialID.text = split[0]
                    tvLotID.text = split[1]
                    tvMaterial.text = split[2]
                    FreeApi_LogUtils.saveErrorMessages(etLot.text.toString(), "绿码转义")
                }
            }

        })

        etPartCode.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(text: Editable) {
                val toString = text.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    etPartCode.setText(toString.replace("\r", "").replace("\n", ""));
                }
            }
        })
    }

    override fun layoutId(): Int {
        return R.layout.activity_material_receive
    }

    override fun onClick(p: View?) {
        when(p?.id){
            R.id.ivBack -> finish()
            R.id.btnSubmit ->{
                if (viewModel.mLotItems.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请先在列表中添加数据",false)
                    return
                }

                viewModel.submit()
            }
            R.id.btnConfirm ->{
                if (etLot.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描绿码",false)
                    etLot.setText("")
                    etLot.requestFocus()
                    return
                }

                if (tvGetMaterial.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请选择领料方式",false)
                    return
                }

                if (tvPutMaterial.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请选择发料库位",false)
                    return
                }

                if (etNum.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请输入数量",false)
                    etNum.setText("")
                    etNum.requestFocus()
                    return
                }

                viewModel.addData(tvGetMaterial.text.toString(),tvPutMaterial.text.toString(),etLot.text.toString(),etPartCode.text.toString(),etNum.text.toString())
            }
            R.id.flGetMaterial ->{
                if (viewModel.getMaterialList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到领料方式信息",false)
                    return
                }

                if (getMaterialPop == null){
                    getMaterialPop = PopWindowsUtils.getOneChoosePop(this, flGetMaterial.width, viewModel.getMaterialList, ivGetMaterial,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvGetMaterial.text = codeS
                        }

                    })

                }

                if (getMaterialPop!!.isShowing) {
                    getMaterialPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivGetMaterial)
                    getMaterialPop?.showAsDropDown(flGetMaterial, 0, 0)
                }
            }
            R.id.flPutMaterial ->{
                if (viewModel.putMaterialList.size <= 0){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("未获取到发料库位信息",false)
                    return
                }

                if (putMaterialPop == null){
                    putMaterialPop = PopWindowsUtils.getOneChoosePop(this, flPutMaterial.width, viewModel.putMaterialList, ivPutMaterial,object : PopWindowsUtils.PopTextAndPositionCallback{
                        override fun onSuccess(codeS: String, potion: Int) {
                            tvPutMaterial.text = codeS
                        }

                    })

                }

                if (putMaterialPop!!.isShowing) {
                    putMaterialPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivPutMaterial)
                    putMaterialPop?.showAsDropDown(flPutMaterial, 0, 0)
                }
            }
        }
    }
}